<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
	include "../../value/value_en.php";
} else if($lang == "VN") {
	include "../../value/value_vn.php";
}
else {
	include "../../value/value_kr.php";
}
include '../../operations/connection.php';
$list=$_SESSION['sort'];
$page=$_GET['page'];
$str=$_GET['str'];
$_SESSION['sortn']=$str;
$co=$_GET['co'];
$rows_per_page = 20 ;
$page_start = ( $page - 1 ) * $rows_per_page; 
$page_end = $page * $rows_per_page;
if($co==1){
	if($list[$str]=="DESC")
		$list[$str]="ASC";
	else
		$list[$str]="DESC";
}

if(isset($_REQUEST['datefrom']) && isset($_REQUEST['dateto']) && isset($_REQUEST['cbcus']) && isset($_REQUEST['search'])){
	if($_REQUEST['search']=="Search"){
		$fromdate=$_REQUEST['datefrom'];
		$todate=$_REQUEST['dateto'];
		$cus=$_REQUEST['cbcus'];
		$search=$_REQUEST['search'];
    	$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney 
    	from approvedoc ,buying_document,employee 
    	where approvedoc.IDEmployee=employee.IDEmployee 
    	and approvedoc.IDTypeDoc in(13,15) and approvedoc.IDStatus=1 
    	and approvedoc.IDApproveDoc=buying_document.document_id ";
    	if($fromdate!="" && $todate!=""){
    		$sql .="AND (approvedoc.Timecreate > '".$fromdate." 00:00:00' AND approvedoc.Timecreate < '".$todate." 23:59:59') ";
    	}else{
    		if($fromdate!="" ){
    			$sql .=" and approvedoc.Timecreate > '".$fromdate." 00:00:00' ";
    		}else if($todate!=""){
    			$sql .=" AND approvedoc.Timecreate < '".$todate." 23:59:59' ";
    		}
    	}
    	
    	if($cus!=""){
			$sql .=" and buying_document.company like '%".$cus."%' ";
		}
		$sql .=" ORDER BY ".$str." ".$list[$str];
    	$mapp=mysql_query($sql);
		$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page );
	}else if($_REQUEST['search']=="SearchALL"){
		$fromdate=$_REQUEST['datefrom'];
		$todate=$_REQUEST['dateto'];
		$search=$_REQUEST['search'];
		$cus=$_REQUEST['cbcus'];
		$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney 
		from approvedoc ,buying_document,employee 
		where approvedoc.IDEmployee=employee.IDEmployee 
		and approvedoc.IDTypeDoc in(13,15)  and approvedoc.IDStatus=1 
		and approvedoc.IDApproveDoc=buying_document.document_id  ORDER BY ".$str." ".$list[$str];
		$mapp=mysql_query($sql);
		$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page );
	}else{
		$fromdate=$_REQUEST['datefrom'];
		$todate=$_REQUEST['dateto'];
		$search=$_REQUEST['search'];
		$cus=$_REQUEST['cbcus'];
		$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney 
		from approvedoc ,buying_document,employee 
		where approvedoc.IDEmployee=employee.IDEmployee 
		and approvedoc.IDTypeDoc in(13,15) and approvedoc.IDStatus=1 
		and approvedoc.IDApproveDoc=buying_document.document_id  AND (approvedoc.Timecreate > '".$fromdate." 00:00:00' AND approvedoc.Timecreate < '".$todate." 23:59:59') 
		ORDER BY ".$str." ".$list[$str];
		$mapp=mysql_query($sql);
		$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page );
	}
}
$_SESSION['sort']=$list;
?>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<div style="font-size:18px; padding-left:10px; margin-bottom:10px; font-weight:bold;"> <?php echo $l_Purchaseorder;?> : <?php echo mysql_num_rows($mapp);?></div>
                            <table class="rf-dt" width="100%">
                                <thead>																
                                    <tr>
                                      <th width="5%" class="rf-dt-shdr-c" style="padding:10px" scope="col"><?php echo $l_no;?></th>
                                      <th width="45%" class="rf-dt-shdr-c" style="padding:10px" scope="col"><a onclick="sort(<?=$page?>,'approvedoc.Tile',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_title;?></a></th>
                                        <th width="10%" class="rf-dt-shdr-c" scope="col"><a onclick="sort(<?=$page?>,'approvedoc.SumMoney',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_total;?></a></th>
                                        <th width="25%" class="rf-dt-shdr-c" scope="col"><a onclick="sort(<?=$page?>,'employee.FullName',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_writer;?></a></th>
                                        <th width="15%" class="rf-dt-shdr-c" scope="col"><a onclick="sort(<?=$page?>,'approvedoc.Timecreate',1,'<?=$search?>');" style="color:#1f71ee"><?php echo $l_created_date;?></a></th>
                                    </tr>
                                </thead>
                                <tbody>
                                	<?php 
                                		if(mysql_num_rows($mapp)>0){
                                			$i=0;
                                			$tong=0;
                                			$stong=0;
                                			while($row = mysql_fetch_array($mapp)){
                                				$tien=0;
                                				if($row[SumMoney]>0){
                                        			$tien=$row[SumMoney];
                                        		}else{
                                        			$s1="select sum(buying_document.total) as tongtien from approvedoc,buying_document where approvedoc.IDApproveDoc=buying_document.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
													$k1=mysql_query($s1);
													while ($r1=mysql_fetch_array($k1)){
														if($r1[tongtien]>0){
															$tien=$r1[tongtien];
															$s2="update approvedoc set approvedoc.SumMoney=".$tien." where approvedoc='".$row[IDApproveDoc]."'";
															mysql_query($s2);
														}
													}
                                        		}
												if ($i < $page_end){ 
													if ( $i >= $page_start ){
	                                				?>
	                                				<tr>
				                                      <td class="rf-dt-c"><?=$i+1?></td>
				                                        <td class="rf-dt-c"><a href="accountant-search.php?id=<?=$row[IDApproveDoc]?>&type=<?=$row[IDTypeDoc]?>&page=<?=$page?>" style="color:#0078D0"><?php echo $row[Tile]?></a></td>
				                                        <td class="rf-dt-c" style="padding:10px;text-align: right;">
				                                        	<?php 
				                                        		echo number_format($tien);
				                                        	?>
				                                        </td>
				                                        <td class="rf-dt-c"><?php echo $row[FullName]?></td>
				                                        <td class="rf-dt-c"><?php echo $row[Timecreate]?></td>
				                                    </tr>
	                                				<?php 
	                                					$tong +=$tien;
													}
		                                			$i++;
												}
												$stong +=$tien;
                                			}
                                			?>
                                			<tr>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo $l_total;?></td>
                                				<td class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo number_format($tong);?></td>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px" scope="col"></td>
                                			</tr>
                                			<tr>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo $l_Purchasetotal;?></td>
                                				<td class="rf-dt-shdr-c" style="padding:10px;text-align: right;" scope="col"><?php echo number_format($stong);?></td>
                                				<td colspan="2" class="rf-dt-shdr-c" style="padding:10px" scope="col"></td>
                                			</tr>
                                			<?php 
                                		}
                                		
                                	?>
                                	
																		
												<?php if ( $number_of_page > 1 ) 
												{ ?>
												<tr>
	                                				<td colspan="5" class="rf-dt-shdr-c" style="padding:10px" scope="col" >
														<span style="background:#fff; padding:1px; display: inline-block;">
												<?php 
													if ($page==1)
														$p=2;
													else if ($page==$number_of_page)
														$p=$number_of_page-1;
													else $p=$page;
													if($page!=1){?>
														<a onclick="sort(1,'<?=$str?>',0,'<?=$search?>');" ><span class='button_next'><?php echo "<<<";?></span> </a>
														<a onclick="sort(<?=($page-1)?>,'<?=$str?>',0,'<?=$search?>');" ><span class='button_next'><?php echo "<<";?></span> </a>
													<?php  }
													else{
														echo "<span class='button_next'><<<</span> "; 
														echo "<span class='button_next'><<</span> ";
													}
													for ( $i = $p-1; $i <= $p+1; $i++ ) {	
															if ($i<=$number_of_page && $i>0)
															
																if ($i==$page){
																	echo "<span class='button_next' style='color:black;'><b>".$i."</b></span>"; 
																}else{
																?>
																	<a onclick="sort(<?=$i?>,'<?=$str?>',0,'<?=$search?>');" ><span class="button_next" style="color: #508e21;">
																	<?php 
																	echo $i;
																	?>
																	</span> </a>
																	<?php 
																}
															?>
														
													<?php 
															}
														
														if($page!=$number_of_page){ ?>
															<a onclick="sort(<?=($page+1)?>,'<?=$str?>',0,'<?=$search?>');"><span class='button_next'><?php echo ">>";?></span> </a>
															<a onclick="sort(<?=($number_of_page)?>,'<?=$str?>',0,'<?=$search?>');" ><span class='button_next'><?php echo ">>>";?></span> </a>
														<?php }
														else{
															echo "<span class='button_next'>>></span> ";
															echo "<span class='button_next'>>>></span> ";
														}
														?>
															</span>
														</td>
					                                </tr>
														<?php 
													}
												
													
													?>
																										
										
                                </tbody>
                                
                            </table>
                            <?php mysql_close($connection);?>